﻿using HBase;
using hCommand;
using Persistence;
using System; 
using Dapper;
using System.Data;
using System.Collections.Generic;
using Persistence.mySql;
namespace Api {
    public class StatTask : ZtTask {
         
        IDoiServer doiServer = WMSDalFactray.getDal<IDoiServer>();

        IStatServer statServer = WMSDalFactray.getDal<IStatServer>();
        ICoreStockServer wmsServer = WMSDalFactray.getDal<ICoreStockServer>();
        ICSDServer csdServer = WMSDalFactray.getDal<ICSDServer>();
        IDoServer doServer = WMSDalFactray.getDal<IDoServer>();
        protected override bool init() {
            time = 3000;
            return true;
        }
        
   
        public bool manual = false;
        void test() {
            var param = new DynamicParameters();

            param.Add("@taskno",20);
            param.Add("@msg","",DbType.String,direction: ParameterDirection.Output);
            doiServer.execProcedure("trans_delete",param);
            String v = param.Get<object>("@msg").ToString();
        }
        public DateTime d= DateTime.Now;
        public override bool excute() {
            var last = DateTime.Now;
            detback2Middle();
            
            statServer.statDoi(); 
            statServer.statDoo();
            statServer.statBatch();
            statServer.statItme();

            statServer.statHisDailyData();
            if (manual) {
                statServer.statDailyData();
                manual = false;
            } 
            detback2guidang();
            return true;
        }

        void detback2Middle() {
            List<CoreStockDet> list = wmsServer.GetAll<CoreStockDet>(" and status > " + (int)StockDetailStatus.上限);
            foreach (CoreStockDet d in list) {
                WMSTransactionFacade.doCsd21Engine("Core_Stock_Det", "d_Stock_Det1", d.id);
            }
        }
        void detback2guidang() {

            DateTime end = DateTime.Now;
            end = end.AddDays(-30);
            List<CoreStockDet> list2 = wmsServer.getAll<CoreStockDet>("d_Stock_Det1", " and out_Time <@end ", new { end });
            foreach (CoreStockDet d1 in list2) {
                List<WhDoo> doos = wmsServer.getData<WhDoo>("select * from Wh_Doo  where id in (select pid from Wh_Doo_det where id=@dooDetId) ", d1);
                if (doos.Count == 1 && doos[0].status != 9) {
                    break;
                }
                List<WhDoi> dois1 = wmsServer.getData<WhDoi>("select * from Wh_Doi  where id in (select pid from Wh_Doi_det where id=@doiDetId) ", d1);
                if (dois1.Count == 1 && dois1[0].status != 9) {
                    break;
                }

                CoreStock cs = wmsServer.getById<CoreStock>(d1.pid);
                if (cs != null) {
                    List<WhDopk> pks = wmsServer.getData<WhDopk>("select * from Wh_Dopk  where id = @pkId ", cs);
                    if (pks.Count == 1 && pks[0].status != 9) {
                        break;
                    }
                }


                WMSTransactionFacade.doCsd21Engine("d_stock_det1", "d_stock_det2", d1.id);
            }
        }

    }


}